package com.ant.task.custom;
import java.io.*;
import java.util.*;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class FileSorter extends Task
{
	private File file, tofile;

	public void execute() throws BuildException
	{
		try
		{
			BufferedReader from = new BufferedReader(new FileReader(file));
			BufferedWriter to = new BufferedWriter(new FileWriter(tofile));
			List allLines = new ArrayList();
			String line = from.readLine();
			while (line != null)
			{
				allLines.add(line);
				line = from.readLine();
			}
			from.close();

			Collections.sort(allLines);

			for (ListIterator i = allLines.listIterator(); i.hasNext();)
			{
				String s = (String)i.next();
				to.write(s);
				to.newLine();
			}
			to.close();

		}
		catch(FileNotFoundException e)
		{
			throw new BuildException(e);
		}
		catch (IOException e)
		{
			throw new BuildException(e);
		}
	}

	public void setFile(File file)
	{
		this.file = file;
	}

	public void setTofile(File tofile)
	{
		this.tofile = tofile;
	}
}
